package com.webapp.taghandlers;

import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.*;

import com.webapp.models.FormBean;

/*
 * A tag handler that iterates through 
 * a selected bean object, stores it attributes
 * into one of the shared scopes and display
 * the information.
 * 
 * Name(s) : Kenny, George, Dan
 * Date: 3/18/2012
 * Assignment: 5
 * Course: CSE 336 : Internet Programming
 */
public class BeanAttributeHandler extends SimpleTagSupport 
{
	FormBean beanObject;
	
	public void doTag() throws JspException, IOException
	{
		List beansPropertyList = beanObject.getList();
		
		PageContext pageContext = (PageContext) getJspContext();
		JspWriter out = pageContext.getOut();
		
		// Iterate through the beanObjectList.
		for (int i = 0; i < beansPropertyList.size(); i++)
		{
			// Store it in a scoped variable.
			// (This might not be the right way to store it. Change
			// it if needed).
			pageContext.getRequest().setAttribute("" + i, beansPropertyList.get(i));
			
			// Print result.
			try 
			{
				out.println(beansPropertyList.get(i) + "<br />");
			}
			catch (IOException ex)
			{
				ex.printStackTrace();
			}
		}
	}
	
	public void setBean(FormBean b)
	{
		this.beanObject = b;
	}
}
